package com.lineage.data.item_etcitem.magicreel;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_EffectLocation;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.Random;

public class FireBadge extends ItemExecutor {
  private final Random _random = new Random();
  
  public static ItemExecutor get() {
    return new FireBadge();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (pc == null)
      return; 
    if (item == null)
      return; 
    L1BuffUtil.cancelAbsoluteBarrier(pc);
    if (pc.isInvisble())
      pc.delInvis(); 
    pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 19));
    int chargeCount = item.getChargeCount();
    if (chargeCount <= 0) {
      if (pc.getInventory().removeItem(item, 1L) == 1L)
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(154)); 
    } else {
      item.setChargeCount(item.getChargeCount() - 1);
      pc.getInventory().updateItem(item, 128);
      int x = pc.getX();
      int y = pc.getY();
      int mapId = pc.getMapId();
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(x + 1, y + 1, mapId), 1819), 7);
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(x - 1, y + 1, mapId), 1819), 7);
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(x - 1, y - 1, mapId), 1819), 7);
      pc.sendPacketsXR((ServerBasePacket)new S_EffectLocation(new L1Location(x + 1, y - 1, mapId), 1819), 7);
      L1PcInstance tgpc = (L1PcInstance)pc.getNowTarget();
      if (tgpc != null && !tgpc.isSafetyZone()) {
        double dmg = (this._random.nextInt(60) + 160) * 1.0D;
        tgpc.receiveDamage((L1Character)pc, dmg, false, false);
        Iterator<L1PcInstance> iterator = World.get().getVisiblePlayer((L1Object)tgpc, 4).iterator();
        while (iterator.hasNext()) {
          L1PcInstance tgClanPc = iterator.next();
          if (tgClanPc.getClanid() == tgpc.getClanid() && !tgpc.isSafetyZone()) {
            tgClanPc.receiveDamage((L1Character)pc, dmg * 0.8D, false, false);
            tgClanPc.broadcastPacketX8((ServerBasePacket)new S_DoActionGFX(tgClanPc.getId(), 2));
          } 
        } 
      } 
      Iterator<L1Object> iterator2 = World.get().getVisibleObjects((L1Object)pc, 5).iterator();
      while (iterator2.hasNext()) {
        L1Object object = iterator2.next();
        if (object instanceof L1MonsterInstance) {
          L1MonsterInstance mob = (L1MonsterInstance)object;
          int dmg2 = this._random.nextInt(60) + 160;
          mob.receiveDamage((L1Character)pc, dmg2);
          mob.broadcastPacketX8((ServerBasePacket)new S_DoActionGFX(mob.getId(), 2));
        } 
      } 
    } 
  }
}
